/*
                  Player Class Implementation
*/
#include "Player.h"

Player::Player()
{
    strName = "";
    bIsCPU = false;
    iTargetFood = -1;
    bNeedsNewPath = true;
    t = NULL;

    iScore = 0;
    iKills = 0;
    iTotalGameScore = 0;
    iDeaths = 0;
    iPersonality = (rand() % 5) * 50;       // 200 is very passive, 0 is very aggressive
}

Player::~Player()
{
    while (! Path.empty())
    {
        Path.pop();
    }

    SDL_FreeSurface(t);
}

int Player::Initialise(Point Position, int iNewWidth, int iNewHeight, int iNewDirection, int iSize)
{
    playerSnake.Initialise(Position, iNewWidth, iNewHeight, iNewDirection, iSize);

    t = IMG_Load("data//sprites//t.png");
}

void Player::setName(const string& strNewName)
{
    strName = strNewName;
}

string Player::getName()
{
    return strName;
}

Snake& Player::getSnake()
{
    return playerSnake;
}

SDL_Rect Player::getPlayerRect()
{
    SDL_Rect PlayerRect;

    PlayerRect.x = (int)playerSnake.getPosition().x;
    PlayerRect.y = (int)playerSnake.getPosition().y;
    PlayerRect.w = playerSnake.getWidth();
    PlayerRect.h = playerSnake.getHeight();

    return PlayerRect;
}

void Player::Draw(SDL_Surface *surface)
{
    playerSnake.Draw(surface);

    /* Overlay paths on map
    queue<int> q = Path;

    SDL_Rect dstRect;
    Point Destination = getPosition();

    while (! q.empty())
    {
        switch (q.front())
        {
            case UP:
                Destination.y -= DISTANCE_MOVE;
            break;

            case DOWN:
                Destination.y += DISTANCE_MOVE;
            break;

            case LEFT:
                Destination.x -= DISTANCE_MOVE;
            break;

            case RIGHT:
                Destination.x += DISTANCE_MOVE;
            break;
        }

        dstRect.x = (int)Destination.x;
        dstRect.y = (int)Destination.y;

        SDL_BlitSurface(t, NULL, surface, &dstRect);

        q.pop();
    }
    */

}

void Player::Move(Map& currentMap)
{
    if (bIsCPU == true)
    {
        if (Path.size() > 0)
        {
            playerSnake.setDirection(Path.front());
            Path.pop();
        }
    }

    playerSnake.Move(currentMap);
}

Point Player::getPosition()
{
    return playerSnake.getPosition();
}

bool Player::isCPU()
{
    return bIsCPU;
}

void Player::setIsCPU(const bool& bNewVal)
{
    bIsCPU = bNewVal;
}

int Player::getTargetFood()
{
    return iTargetFood;
}

void Player::setTargetFood(const int& iNewTarget)
{
    iTargetFood = iNewTarget;
}

bool Player::getNeedsNewPath()
{
    return bNeedsNewPath;
}

void Player::setNeedsNewPath(bool bNewVal)
{
    bNeedsNewPath = bNewVal;
}

queue<int> Player::getPath()
{
    return Path;
}

void Player::setPath(queue<int> NewPath)
{
    while (! Path.empty())
    {
        Path.pop();
    }

    Path = NewPath;
}

int Player::getNextMove()
{
    return Path.front();
}

int Player::Reset(Point Position, int iNewDirection, int iSize)
{
    iTargetFood = -1;
    bNeedsNewPath = true;

    iScore = 0;

    return playerSnake.Reset(Position, iNewDirection, iSize);
}

void Player::AddScore(const int& iAmount)
{
    iScore += iAmount;

    if (iAmount > 0)
    {
        iTotalGameScore += iAmount;
    }
}

void Player::setScore(const int& iNewScore)
{
    iScore = iNewScore;
}

int Player::getScore()
{
    return iScore;
}

void Player::AddKills(const int& iAmount)
{
    iKills += iAmount;
}

void Player::setKills(const int& iNewKills)
{
    iKills = iNewKills;
}

int Player::getKills()
{
    return iKills;
}

long Player::getTotalScore()
{
    return iTotalGameScore;
}

void Player::AddDeaths(const int& iAmount)
{
    iDeaths += iAmount;
}

int Player::getDeaths()
{
    return iDeaths;
}

void Player::setDeaths(const int& iNewDeaths)
{
    iDeaths = iNewDeaths;
}

int Player::getPersonality()
{
    return iPersonality;
}
